package com.wisnews.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.wisnews.pojo.Input;
import com.wisnews.pojo.Result;
import com.wisnews.pojo.sr.entity.Member;
import com.wisnews.service.Impl.WisdomPushImpl;
import com.wisnews.service.sr.Impl.MemberServiceImpl;
import com.wisnews.util.Constants;
import com.wisnews.util.FileUploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

/**
 * @Description：智能推送前端控制器
 * @Author: yuyd
 * @Email: yyd8358@foxmail.com
 * @Date: 2021/2/23 16:21
 * @Version 1.0
 */
@RestController
@Slf4j
public class WisdomPushController {

    @Autowired
    private MemberServiceImpl memberService;

    @Autowired
    private WisdomPushImpl wisdomPush;

    @RequestMapping(value = "/wisdomPush",method = RequestMethod.POST)
    public Result wisdomPush(@RequestBody Input newsInput, HttpServletRequest request){
        IPage iPage;
       // 检查登录信息
        Member user = FileUploadUtil.getLoginMemberInfo(request);

        try {
            if (ObjectUtils.isNull(user)){
                return new Result(Constants.SYS_CODE.FAIL, Constants.SYS_CODE.FAIL_MESSAGE);
            }
            // 获取针对当前登录的前台用户的数据
            iPage = wisdomPush.pushDevice(user, newsInput.toPage());
        }catch (SQLException e){
            log.error(e.toString());
            return new Result(Constants.SYS_CODE.FAIL, Constants.SYS_CODE.FAIL_MESSAGE);
        }

        return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE,iPage);
    }


    @RequestMapping(value = "/importantNews",method = RequestMethod.POST)
    public Result importantNews(@RequestBody Input newsInput){
            IPage iPage;
            try {
                 iPage = wisdomPush.pushImportantNews(newsInput.toPage());
            }catch (SQLException e){
                log.error(e.toString());
                return new Result(Constants.SYS_CODE.FAIL,Constants.SYS_CODE.FAIL_MESSAGE);
            }
                return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE, iPage);
        }


}
